<?php
include ("../include/config.php");

require($config['basedir']."/include/authenticate.php");
require($config['basedir']."/admin/classes/trivia.class.php");
//chk_permission('poll');
$obj_trivia = new Trivia();
$_action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
$post = $_REQUEST;
chk_permission('poll','view');
switch($_action) {
    case 'remove_trivia_option': 
	     //print_r($post);exit; 
		 echo  $obj_trivia->removeTriviaOptions($post);	
	break;	 
	case 'feature_poll':
	   chk_permission('trivia','update');	
	   $obj_trivia->pollFeatured($post['question_id']);	
	break;
	case 'add':
         
		
		
		//print_r($post);exit;
		if (isset($_POST['submit']))
		{
			$obj_trivia->addTrivia($post);
		}
		stemplate::assign("category_dropdown", $obj_trivia->getCategoriesDropDown($post));
		stemplate::assign("level_dropdown", $obj_trivia->getQuestionLevelDropDown($post));
		stemplate::assign("polls", $getPolls);
		//print_r($parent_menu_list);exit;
		stemplate::assign("post", $post);
		stemplate::display("admin/trivia/save.tpl");
		
	break;
	 
	case 'edit':
		$id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : 0;
		$trivia_data = $obj_trivia->getTriviasByQuestionId($id);
		
		$trivia_options = $obj_trivia->getTriviaOptionByQuestionId($id);
		$options  = $obj_trivia->getQuizOptionAnswer($id);
		$option_answer    = ($options['quiz_option']);
		$new_page_limit = isset($_REQUEST['new_page_limit']) ? $_REQUEST['new_page_limit'] : 0;
		stemplate::assign("id", $id);
		$pg = isset($_REQUEST['pg']) ? $_REQUEST['pg'] : 0;
		$ipp = isset($_REQUEST['ipp']) ? $_REQUEST['ipp'] : 0;
		$poll_question =$poll_question;
		
		stemplate::assign("category_dropdown", $obj_trivia->getCategoriesDropDown($trivia_data));
		stemplate::assign("level_dropdown", $obj_trivia->getQuestionLevelDropDown($trivia_data));
		
		stemplate::assign("trivia_options", $trivia_options);
		stemplate::assign("pg", $pg);
		stemplate::assign("ipp", $ipp);
		stemplate::assign("post", $trivia_data);
		stemplate::assign("option_answer", $option_answer);
		stemplate::assign("new_page_limit", $new_page_limit);
		stemplate::display("admin/trivia/save.tpl");
	break;
	
	case 'update':
		chk_permission('trivia','update');	
		if( $obj_trivia->updateTrivia($post) ) 
		{
			redirect("admin/trivia/save.tpl?updated=1");			
		}
		else
		{
			$id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : 0;
		    stemplate::assign("post", $post);
			stemplate::display("admin/trivia/save.tpl");
		}
	
	break;
	
	case 'delete':
	    chk_permission('trivia','delete');
		$id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : 0;
		$obj_trivia->deleteTrivia($id);
		$trivia = $obj_trivia->getTrivias($post);
		global $pager;
		stemplate::assign("post", $post);
		stemplate::assign("total_records", $pager->items_total);
  		stemplate::assign("pagination_start", $pager->low);
  		stemplate::assign("pagination",$pager->display_pages());
		stemplate::assign('trivia', $trivia);
		stemplate::display("admin/trivia/list.tpl");
		
	break;	
	
	
	case 'change_status':
	   
		//print_r($post);exit;
		chk_permission('trivia','update');
		if($post['status'] == 1){
			$obj_trivia->changePollStatus($post['question_id'], 1);
		} else{
			$obj_trivia->changePollStatus($post['question_id'], 0);
		}

	break;
	
	case 'view_report':
	
		global $pager;
		$report_data =	
		
		
		//varDump($report_data); exit;
		
		stemplate::assign("post", $post);
		stemplate::assign("total_records", $pager->items_total);
  		stemplate::assign("pagination_start", $pager->low);
  		stemplate::assign("pagination",$pager->display_pages());
		stemplate::assign("page_limit_select", $pager->display_page_limits());
		stemplate::assign('report_data', $report_data);
		stemplate::display("admin/trivia/report.tpl");		
			
	
	break;
	
	case 'changeStatus':
		$obj_trivia->changeStatus($post);
	break;
	
	case 'view':
	chk_permission('trivia','view');
	default:
		
		
		
		if($_REQUEST['added'] == 1)	{
			global $msgOk;
			$msgOk = 'Trivia question added successfully.';
		}elseif($_REQUEST['updated'] == 1){
			global $msgOk;
			$msgOk = 'Trivia question updated successfully.';
		}elseif($_REQUEST['deleted'] == 1){
			global $msgOk;
			$msgOk = 'Trivia question deleted successfully.';
		}elseif($post['status'] == 1){
			global $msgOk;
			$msgOk = 'Trivia question actived successfully.';	
		}elseif(isset($post['status']) && $post['status'] == 0){
			global $msgOk;
			$msgOk = 'Trivia question deactived successfully.';
		}
		
		
		$trivia = $obj_trivia->getTrivias($post);
		//print_r($trivia);exit;
		global $pager;
		
		stemplate::assign("post", $post);
		stemplate::assign("total_records", $pager->items_total);
  		stemplate::assign("pagination_start", $pager->low);
  		stemplate::assign("pagination",$pager->display_pages());
		stemplate::assign("page_limit_select", $pager->display_page_limits());
		stemplate::assign('trivia', $trivia);
		stemplate::display("admin/trivia/list.tpl");
		
    break; 

}




?>